package com.adv.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.adv.mapper.AdvertimgMapper;
import com.adv.mapper.AdvertposMapper;
import com.adv.model.Advertimg;
import com.adv.model.AdvertimgSqlParam;
import com.adv.model.Advertpos;
import com.adv.model.AdvertposSqlParam;
import com.adv.util.Constant;
import com.adv.util.DozerUtil;
import com.adv.vo.LogoForm;

@Component
public class LogoService {

	@Autowired
	private AdvertposMapper mapper;

	@Autowired
	private AdvertimgMapper imgMapper;

	public LogoForm getOnlyLogo() {
		AdvertposSqlParam param = new AdvertposSqlParam();
		param.createCriteria().andTypeEqualTo(Constant.AdverposType.LOGO.getCode());
		List<Advertpos> list = mapper.selectByParam(param);
		if (!CollectionUtils.isEmpty(list)) {
			LogoForm form = DozerUtil.mapObj(list.get(0), LogoForm.class);
			if (null != form && !CollectionUtils.isEmpty(list.get(0).getAdvertimgList())) {
				form.setIcon(list.get(0).getAdvertimgList().get(0).getIcon());
			}
			return form;
		}
		return null;
	}

	public LogoForm getLogoByPrimaryId(Integer id) {
		Advertpos advertpos = mapper.selectByPrimaryKey(id);
		LogoForm form = DozerUtil.mapObj(advertpos, LogoForm.class);
		if (null != form && !CollectionUtils.isEmpty(advertpos.getAdvertimgList())) {
			form.setIcon(advertpos.getAdvertimgList().get(0).getIcon());
		}
		return form;
	}

	// 新建和保存消息
	public Map<String, Object> saveLogo(LogoForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form) {
			if (StringUtils.isEmpty(form.getRegistertime())) {
				form.setRegistertime(Constant.SDFCOMMON.format(new Date()));
			}
			form.setType(Constant.AdverposType.LOGO.getCode());
			Advertpos record = DozerUtil.mapObj(form, Advertpos.class);
			if (null != form.getId()) {
				int num = mapper.updateByPrimaryKeySelective(record);
				if (num == 1) {
					num = insertAdvertimg(form, form.getId());
					if (num == 1) {
						resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
					}
				}
			} else {
				int num = mapper.insertSelective(record);
				if (num == 1) {
					num = insertAdvertimg(form, record.getId());
					if (num == 1) {
						resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
					}
				}
			}
		}
		return resultMap;
	}

	private int insertAdvertimg(LogoForm form, int id) {
		AdvertimgSqlParam param = new AdvertimgSqlParam();
		param.createCriteria().andLinkidEqualTo(id);
		List<Advertimg> advertimgList = imgMapper.selectByParam(param);
		if (CollectionUtils.isEmpty(advertimgList)) {
			Advertimg advertimg = new Advertimg();
			advertimg.setLinkid(id);
			advertimg.setIcon(form.getIcon());
			advertimg.setImgtype(Constant.ImageType.ICON.getCode());
			advertimg.setBusinesstype(Constant.BusinessType.LOGO.getCode());
			return imgMapper.insertSelective(advertimg);
		} else {
			Advertimg advertimg = advertimgList.get(0);
			advertimg.setIcon(form.getIcon());
			return imgMapper.updateByPrimaryKey(advertimg);
		}
	}

	// 修改Logo
	public Map<String, Object> editLogo(LogoForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			Advertpos record = DozerUtil.mapObj(form, Advertpos.class);
			int num = mapper.updateByPrimaryKeySelective(record);
			if (num == 1) {
				resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
			}
		}
		return resultMap;
	}

	// 删除logo
	public Map<String, Object> deleteLogo(LogoForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			AdvertposSqlParam param = new AdvertposSqlParam();
			param.createCriteria().andParentidEqualTo(form.getId());
			List<Advertpos> list = mapper.selectByParam(param);
			if (CollectionUtils.isEmpty(list)) {
				int num = mapper.deleteByPrimaryKey(form.getId());
				if (num == 1) {
					AdvertimgSqlParam imgParam = new AdvertimgSqlParam();
					imgParam.createCriteria().andLinkidEqualTo(form.getId());
					imgMapper.deleteByParam(imgParam);
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			} else {
				resultMap.put(Constant.RESULTKEY, Constant.HASRELATEDRESOURCE);
			}
		}
		return resultMap;
	}

}